package com.wondertek.service.mapper.business;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wondertek.service.dto.app.req.InstallerPageDeviceReq;
import com.wondertek.service.dto.app.req.InstallerPageRoomReq;
import com.wondertek.service.dto.app.res.AppDevicePageByUserIdRes;
import com.wondertek.service.dto.app.res.InstallerRoomRes;
import com.wondertek.service.dto.app.res.KvRes;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author wangyu
 */
public interface InstallerMapper {


    /**
     * 根据操作员userid查询所属站点
     * @param userId
     * @return
     */
    @Select("select id,site_name as name from site s where is_delete = 0 and exists ( select 1 from user_site us where user_id = #{userId} and us.site_id = s.id )")
    List<KvRes> findAllSiteByUserId(@Param("userId") String userId);

    /**
     * 根据siteId查询设备
     * @param req
     * @param page
     * @return
     */
    @Select("select b.status,b.id,b.device_code code,b.device_name name,dtd.category type,sn_code,sk_code, '' roomName,'' deviceSn,'' accessoryType ,s.site_name siteName from device b left join site s on s.id=b.site_id left join device_type_detail dtd on dtd.id=b.device_type_id where b.is_delete=0 and b.site_id=#{req.siteId}")
    Page<AppDevicePageByUserIdRes> pageDeviceBySiteId(InstallerPageDeviceReq req, Page page);


    /**
     * 根据deviceId查询分区
     * @param req
     * @return
     */
    @Select("select room_picture,room_name,r.id as roomId,d.sn_code,r.room_code from room r left join device d on r.device_id = d.id where r.device_id = #{req.deviceId} and r.is_delete = 0")
    Page<InstallerRoomRes> pageRoomByDeviceId(InstallerPageRoomReq req, Page page);


    @Select("select sk_code from device where is_delete = 0 and sn_code = #{snCode} ")
    String findDeviceNum(@Param("snCode") String snCode);

    @Select("select count(1) from accessory where is_delete = 0 and sn_code = #{snCode}")
    Integer findAccessoryNum(@Param("snCode") String snCode);
}
